#include "Camera.h"
#include <iostream>


Camera::Camera()
{
	camID=-1;
	device = new char[20];
	conn = curl_easy_init();
}

Camera::~Camera()
{

	delete device;
	curl_easy_cleanup(conn); 
	cameraRelease();
 
}
void Camera::camInit(int camID, char* camSrc,int camWidth,int camHeight)
{

	IplImage* image;
	int camSrcOpenCV;

	if(camID!=-1)
	{
		this->camWidth = camWidth;
		this->camHeight = camHeight;

		switch(camID)
		{
			//LOCAL camera
			case 0:
			
			camLocation = CAMERA_SRC_LOCAL;
			device = camSrc; 
			
			cout<<device<<endl;
			
			//use opencv camera capture function
			sscanf(camSrc,"%d",&camSrcOpenCV);
			this->cameraDev = cvCaptureFromCAM(camSrcOpenCV);
			
			if(cameraDev != NULL)
			{
				camWidth = (int) cvGetCaptureProperty( cameraDev, CV_CAP_PROP_FRAME_WIDTH );
				camHeight = (int) cvGetCaptureProperty( cameraDev, CV_CAP_PROP_FRAME_HEIGHT );
				printf("%d, %d\n",camWidth, camHeight);

				isOn=true;
				image = cvQueryFrame(cameraDev);
				actualImg = cvCreateImage(cvGetSize(image),8,3);
			}
			else
			{
				printf("ERROR: No camera under that index!!\n");
			}

			
			break;

			//REMOTE camera			  
			case 1:
	  
	  		camLocation = CAMERA_SRC_REMOTE;
	  		//device = camSrc;

	  		printf("%s\n",camSrc);
	  		sprintf((char*)device,"%s:8080/?action=snapshot", camSrc);

	  		printf("aca\n");

	  		printf("%s\n",device);
			
			cout<<device<<endl;

			isOn=true;
			actualImg = cvCreateImage(cvSize(camWidth,camHeight),8,3);	
			break;
		}
	}
}

void Camera::cameraRelease()
{
	if(isOn){
		switch(camLocation)
		{
			case CAMERA_SRC_LOCAL:
			cvReleaseCapture(&cameraDev);
			break;
			
			case CAMERA_SRC_REMOTE:
			break;
		}
		isOn=false;
	}

}

IplImage* Camera::queryImage()
{
	IplImage* image;
	FILE *fp; 
	CURLcode result; 
	
	
	if(camLocation == CAMERA_SRC_LOCAL)
		image =cvCreateImageHeader(cvSize(camWidth,camHeight),8,3);
	else
	{
		//image = cvCreateImage(cvSize(camWidth,camHeight),8,3);
		fp = fopen("img.jpg", "wb"); 
	}	
	
	if(isOn)
	{
		switch(camLocation)
		{
			case CAMERA_SRC_LOCAL:
		

			// Grab the next frame (blocking)
			actualImg = cvQueryFrame(cameraDev);
			
			//cvCopy(image,actualImg);
			
			//cvReleaseImageHeader(&image);
			return actualImg;

			break;
			
			case CAMERA_SRC_REMOTE:
			
			
			if(conn)
			{
				curl_easy_setopt(conn, CURLOPT_WRITEFUNCTION, NULL);
				curl_easy_setopt(conn, CURLOPT_VERBOSE,0);
				curl_easy_setopt(conn, CURLOPT_URL, device);
				curl_easy_setopt(conn, CURLOPT_WRITEDATA, fp);
				
				// Grab image
				result = curl_easy_perform(conn);
	            if(result){
	                cout << "Cannot grab the image!"<<endl;
	                return NULL;   
	            }
				
				fclose(fp); 
				
				
				//now copy image to actualImg
				image = cvLoadImage( "img.jpg",1);

				if(image->width != actualImg->width || image->height != actualImg->height)
				{

					cvReleaseImage(&actualImg);
					actualImg = cvCreateImage(cvSize(image->width,image->height),8,3);

					camWidth = image->width;
					camHeight = image->height;

				}


				cvCopy(image,actualImg);
				
				cvReleaseImage(&image);
				
				return actualImg;
            }
            else
            	cout<<"NO conexion CURL"<<endl;
            return NULL;
			break;
		}
	}
	else
		return NULL;
	
}

int Camera::getCamWidth()
{
	return camWidth;
}

int Camera::getCamHeight()
{
	return camHeight;
}

bool Camera::isCamOn()
{
	return isOn;
}
